package services;

import domain.entities.Droga;
import domain.entities.DrogaDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: vgonzalez
 * Date: 30/08/12
 * Time: 16:24
 * To change this template use File | Settings | File Templates.
 */

@Service
public class DrogaService<Droga> implements CRUDService<Droga> {

    private DrogaDAO drogaDAO;

    public DrogaService() {
    }

    @Autowired
    public DrogaService(DrogaDAO drogaDAO) {
        this.drogaDAO= drogaDAO;
    }

    public List<Droga> getDrogas(){
        return drogaDAO.findAll();
    }

    public void save(domain.entities.Droga droga){
        this.drogaDAO.attachDirty(droga);
    }

    public domain.entities.Droga findByDrugName(String name){
        List<domain.entities.Droga> drogas = drogaDAO.findByDrugName(name);
        if(drogas.isEmpty())
            return null;
        else
            return drogas.get(0);
    }

    public domain.entities.Droga findByDrugNumber(String number){
        List<domain.entities.Droga> drogas = drogaDAO.findByDrugNumber(number);
        if(drogas.isEmpty())
            return null;
        else
            return drogas.get(0);
    }

    public domain.entities.Droga findById(Integer id){
        return drogaDAO.findById(id);
    }

    @Override
    public <V extends Criteria> List<Droga> list(V criteria) {
        List<Droga> list = drogaDAO.findAll();
        Iterator<Droga> it = list.iterator();
        while(it.hasNext()){
            if(!criteria.isIn(it.next())){
                it.remove();
            }
        }
        return list;
    }
}
